package club.westudy.java.demo401;

import java.util.EmptyStackException;

/**
 * 一个简单的基于数组实现的栈结构
 * @param <T>
 */
public class ArrayStack1<T> implements IStack<T> {
    private Object[] elements = null;
    private int size = 0;

    public ArrayStack1() {
        this.elements = new Object[8];
    }


    public void push(T val) {
        this.elements[size++] = val;
    }

    public T peek() {
        int index = size - 1;
        if (index < 0) {
            throw new EmptyStackException();
        }
        return (T) this.elements[index];
    }

    public T pop() {
        T val = peek();
        size--;
        return val;
    }

    public int size() {
        return this.size;
    }

    public boolean isEmpty() {
        return size() == 0;
    }

    public void clear() {
        this.size = 0;
        this.elements = new Object[8];
    }

}